﻿/*
 * Copyright 2014 Francois Karman
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using Microsoft.Build.Framework;
using System;
using System.Xml;

namespace Quartz.WebManager.BuildExt
{
    /// <summary>
    /// Merges multiple xmldocs into one single file.
    /// </summary>
    public class MergeXmlDocTask : ITask
    {
        /// <summary>
        /// Gets or sets the xml files that should be merged.
        /// </summary>
        [Required]
        public ITaskItem[] InputFiles { get; set; }

        /// <summary>
        /// Gets or sets the path of the result file.
        /// </summary>
        [Required]
        public string OutputFile { get; set; }

        /// <summary>
        /// Gets or sets the build engine associated with the task.
        /// </summary>
        public IBuildEngine BuildEngine { get; set; }

        /// <summary>
        /// Gets or sets any host object that is associated with the task.
        /// </summary>
        public ITaskHost HostObject { get; set; }

        /// <summary>
        /// Executes the task.
        /// </summary>
        /// <returns>
        /// <c>true</c> if the task executed successfully; otherwise, <c>false</c>.</returns>
        /// <remarks>
        /// This method is called by the build engine to execute a task.
        /// Upon termination of this method, a task should indicate if the execution was successful.
        /// If a task throws an exception from this method, the engine assumes that the task has failed.
        /// </remarks>
        public bool Execute()
        {
            // Create the base structure
            XmlDocument result = new XmlDocument();
            result.AppendChild(result.CreateXmlDeclaration("1.0", "UTF-8", null));
            XmlElement doc = result.CreateElement("doc");
            XmlElement members = result.CreateElement("members");
            doc.AppendChild(members);
            result.AppendChild(doc);

            // Read all the provided xml files and extract the doc/members/member elements
            foreach (ITaskItem item in this.InputFiles)
            {
                XmlDocument input = new XmlDocument();
                input.Load(item.ItemSpec);
                foreach(XmlNode node in input.GetElementsByTagName("members"))
                {
                    foreach(XmlNode member in node.ChildNodes)
                    {
                        XmlNode clone = result.ImportNode(member, true);
                        members.AppendChild(clone);
                    }
                }
            }

            // Save the results
            result.Save(this.OutputFile);

            return true;
        }

    }
}
